package cn.cs.yangtai.controller;

import cn.cs.yangtai.entity.Score;
import cn.cs.yangtai.entity.vo.PageResult;
import cn.cs.yangtai.entity.vo.ResponsePojo;
import cn.cs.yangtai.service.ScoreService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * (Score)表控制层
 *
 * @author makejava
 * @since 2024-12-17 13:12:11
 */
@RestController
@CrossOrigin
@RequestMapping("score")
@Tag(name = "用户评分")
public class ScoreController {
    /**
     * 服务对象
     */
    @Resource
    private ScoreService scoreService;

    /**
     * 分页查询所有用户评分
     *
     * @param cuttentPage 本页
     * @param pageSize    分页大小
     * @return 查询结果
     */
    @GetMapping
    @Operation(summary = "分页查询所有用户评分")
    public ResponsePojo<PageResult<Score>> queryByPage(Integer cuttentPage, Integer pageSize) {
        PageResult<Score> result = scoreService.queryByPage(cuttentPage, pageSize);
        return ResponsePojo.success(result);
    }

    /**
     * 用户评分
     * @return
     */
    @PostMapping("RatingScore")
    @Operation(summary = "用户评分")
    public ResponsePojo<Score> RatingScore(Integer scoreUserId,Integer scoreEXpertId,Integer score,String scoreContent) {
        Score score1=new Score();
        score1.setScoreUserId(scoreUserId);
        score1.setScoreEXpertId(scoreEXpertId);
        score1.setScore(score);
        score1.setScoreContent(scoreContent);
        Score abc= scoreService.addScore(score1);
        if (abc == null) {
            return ResponsePojo.fail(null, "评分失败");
        } else {
            return ResponsePojo.success(score1, "评分成功");
        }

    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @Operation(summary = "根据专家ID查询用户评分")
    public ResponseEntity<Score> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.scoreService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param score 实体
     * @return 新增结果
     */
//    @PostMapping
    public ResponseEntity<Score> add(Integer scoreUserId,Integer scoreEXpertId,Integer score,String scoreContent) {
        Score score1=new Score();
        score1.setScoreUserId(scoreUserId);
        score1.setScoreEXpertId(scoreEXpertId);
        score1.setScore(score);
        score1.setScoreContent(scoreContent);
        return ResponseEntity.ok(this.scoreService.insert(score1));
    }

    /**
     * 编辑数据
     *
     * @param score 实体
     * @return 编辑结果
     */
    @PutMapping
    @Operation(summary = "修改用户评分")
    public ResponseEntity<?> edit(Integer scoreId, Integer scoreUserId, Integer score, String scoreContent) {
        Score score1=scoreService.queryById(scoreId);
        if (score1==null){
            return ResponseEntity.ok().body("评分不存在");
        }
        if (!Objects.equals(scoreUserId,score1.getScoreUserId())){
            return ResponseEntity.ok().body("你无权修改此评分");
        }
        score1.setScoreId(scoreId);
        score1.setScore(score);
        score1.setScoreContent(scoreContent);
        Score result=scoreService.update(score1);
        if (result != null) {
            return ResponseEntity.ok().body("修改成功");
        } else {
            return ResponseEntity.ok().body("修改失败");
        }
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<?> deleteById(Integer id, Integer scoreUserId) {
        Score score1=scoreService.queryById(id);
        if (score1==null){
            return ResponseEntity.ok().body("评分不存在");
        }
        if (!Objects.equals(scoreUserId,score1.getScoreUserId())){
            return ResponseEntity.ok().body("你无权删除此评分");
        }
        Boolean result=scoreService.deleteById(id);
        if (result != null) {
            return ResponseEntity.ok().body("删除成功");
        } else {
            return ResponseEntity.ok().body("删除失败");
        }
    }

}

